//package com.xbongbong.outerlink;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.xbongbong.paas.constant.MessageConstant;
//import com.xbongbong.paas.constant.PaasConstant;
//import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
//import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
//import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
//import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
//import com.xbongbong.paas.elasticsearch.util.EsHelper;
//import com.xbongbong.paas.elasticsearch.util.EsUtil;
//import com.xbongbong.paas.enums.ConditionEnum;
//import com.xbongbong.paas.enums.DelEnum;
//import com.xbongbong.paas.enums.FieldTypeEnum;
//import com.xbongbong.paas.enums.IndexTypeEnum;
//import com.xbongbong.paas.enums.OuterLinkFilterRemoveAttrEnum;
//import com.xbongbong.paas.enums.PlatFormEnum;
//import com.xbongbong.paas.factory.OptionFactory;
//import com.xbongbong.paas.field.FieldAttrEntity;
//import com.xbongbong.paas.field.ItemPoJo;
//import com.xbongbong.paas.field.MultiUnitItemPoJo;
//import com.xbongbong.paas.field.SubFormPoJo;
//import com.xbongbong.paas.handle.AbstractOptionHandle;
//import com.xbongbong.paas.help.FormExplainHelp;
//import com.xbongbong.paas.outerlink.AbstractPaasOuterLinkService;
//import com.xbongbong.paas.pojo.ProductFieldPojo;
//import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
//import com.xbongbong.paas.pojo.dto.FormDataListDTO;
//import com.xbongbong.paas.pojo.dto.OuterLinkAddDTO;
//import com.xbongbong.paas.pojo.dto.OuterLinkAddGetDTO;
//import com.xbongbong.paas.pojo.dto.OuterLinkBaseDTO;
//import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
//import com.xbongbong.paas.pojo.vo.AfterSaveVO;
//import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
//import com.xbongbong.paas.pojo.vo.FormDataAddVO;
//import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
//import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
//import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
//import com.xbongbong.paas.redis.RedisPrefixConstant;
//import com.xbongbong.paas.service.ProModelService;
//import com.xbongbong.paas.toolbox.exception.XbbException;
//import com.xbongbong.paas.toolbox.util.BeanUtil;
//import com.xbongbong.paas.toolbox.util.CollectionsUtil;
//import com.xbongbong.paas.toolbox.util.DateTimeUtil;
//import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
//import com.xbongbong.pro.domain.entity.FormConfigEntity;
//import com.xbongbong.pro.enums.SaasMarkEnum;
//import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
//import com.xbongbong.pro.form.pojo.dto.ChildProductDTO;
//import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
//import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
//import com.xbongbong.pro.purchaseinvoice.dto.PurchaseInvoiceAmountDetailGetDTO;
//import com.xbongbong.pro.purchaseinvoice.vo.PurchaseInvoiceAmountDetailVO;
//import com.xbongbong.pro.workorder.pojo.TimeConsumingOutPojo;
//import com.xbongbong.pro.workorder.pojo.vo.WorkOrderDataEsListVO;
//import com.xbongbong.saas.constant.BasicConstant;
//import com.xbongbong.saas.constant.InvoiceConstant;
//import com.xbongbong.saas.constant.StringConstant;
//import com.xbongbong.saas.domain.entity.InvoiceRelationshipEntity;
//import com.xbongbong.saas.domain.entity.ProductCategoryEntity;
//import com.xbongbong.saas.domain.entity.WorkOrderEntity;
//import com.xbongbong.saas.domain.entity.WorkOrderStageEntity;
//import com.xbongbong.saas.domain.entity.WorkOrderTemplateEntity;
//import com.xbongbong.saas.domain.entity.ext.WorkOrderEntityExt;
//import com.xbongbong.saas.enums.ChildProductFieldEnum;
//import com.xbongbong.saas.enums.FormConfigEnum;
//import com.xbongbong.saas.enums.SelectProductEnum;
//import com.xbongbong.saas.enums.WorkOrderNodeStatusEnum;
//import com.xbongbong.saas.enums.XbbRefTypeEnum;
//import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
//import com.xbongbong.saas.enums.business.InvoiceEnum;
//import com.xbongbong.saas.enums.business.PaySheetEnum;
//import com.xbongbong.saas.enums.business.PaymentEnum;
//import com.xbongbong.saas.enums.business.PaymentSheetEnum;
//import com.xbongbong.saas.enums.business.ProductEnum;
//import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
//import com.xbongbong.saas.enums.business.SupplierCommunicateEnum;
//import com.xbongbong.saas.enums.business.WorkOrderEnum;
//import com.xbongbong.saas.help.InvoiceCommonHelp;
//import com.xbongbong.saas.help.SaasFormHelp;
//import com.xbongbong.saas.help.SaasGetHelp;
//import com.xbongbong.saas.help.SaasListHelp;
//import com.xbongbong.saas.help.SaasSaveHelp;
//import com.xbongbong.saas.help.SaasUpdateHelp;
//import com.xbongbong.saas.model.DataDictionaryModel;
//import com.xbongbong.saas.model.FormConfigModel;
//import com.xbongbong.saas.model.InvoiceRelationshipModel;
//import com.xbongbong.saas.model.PaasFormDataModel;
//import com.xbongbong.saas.model.PayPlanSheetModel;
//import com.xbongbong.saas.model.ProductCategoryModel;
//import com.xbongbong.saas.model.WorkOrderModel;
//import com.xbongbong.saas.model.WorkOrderTemplateModel;
//import com.xbongbong.saas.model.WorkReportModel;
//import com.xbongbong.saas.service.ProductService;
//import com.xbongbong.saas.service.PurchaseInvoiceService;
//import com.xbongbong.saas.service.WorkOrderFormDataService;
//import com.xbongbong.saas.service.WorkOrderTemplateService;
//import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
//import com.xbongbong.workorder.service.WorkOrderBasicService;
//import com.xbongbong.workorder.service.WorkOrderStageService;
//import lombok.extern.slf4j.Slf4j;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.springframework.lang.NonNull;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//import java.util.Set;
//
//import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;
//import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;
//import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
//import static org.elasticsearch.index.query.QueryBuilders.termsQuery;
//
///**
// * pro 默认外链接口类 （备份，拆分失败就用这个）
// * 时间有限，先糅杂在这里，以后各个业务分开
// * @author zheng.li
// **/
//@Slf4j
//@Service
//public class ProOuterLinkServiceImplBackup extends AbstractPaasOuterLinkService {
//
//    @Resource
//    private WorkOrderTemplateService workOrderTemplateService;
//
//    @Resource
//    private WorkOrderStageService workOrderStageService;
//
//    @Resource
//    private PaasFormDataModel paasFormDataModel;
//
//    @Resource
//    private SaasGetHelp saasGetHelp;
//
//    @Resource
//    private DataDictionaryModel dataDictionaryModel;
//
//    @Resource
//    private EsHelper esHelper;
//
//    @Resource
//    private SaasFormHelp saasFormHelp;
//
//    @Resource
//    private OptionFactory optionFactory;
//
//    @Resource
//    private WorkOrderTemplateModel workOrderTemplateModel;
//
//    @Resource
//    private WorkOrderBasicService workOrderBasicService;
//
//    @Resource
//    private WorkOrderModel workOrderModel;
//
//    @Resource
//    private SaasUpdateHelp saasUpdateHelp;
//
//    @Resource
//    private FormExplainHelp formExplainHelp;
//
//    @Resource
//    private ProductService productService;
//
//    @Resource
//    private InvoiceRelationshipModel invoiceRelationshipModel;
//
//    @Resource
//    private SaasListHelp saasListHelp;
//
//    @Resource
//    private ProductCategoryModel productCategoryModel;
//
//    @Resource
//    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;
//
//    @Resource
//    private PurchaseInvoiceService purchaseInvoiceService;
//
//    @Resource
//    private PayPlanSheetModel payPlanSheetModel;
//
//    @Resource
//    private FormConfigModel formConfigModel;
//
//    @Resource
//    private SaasSaveHelp saasSaveHelp;
//
//    @Resource
//    private ProModelService proModelService;
//
//    @Resource
//    private WorkOrderFormDataService workOrderFormDataService;
//
//    @Override
//    public PaasFormDataEntityExt getFormData(Long dataId, String corpid, Integer businessType) throws XbbException {
//        PaasFormDataEntityExt paasFormDataEntityExt = new PaasFormDataEntityExt();
//        if ( Objects.equals(businessType,0) ) {
//            paasFormDataEntityExt = paasFormDataModel.getByKey(dataId,corpid);
//        } else if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode())) {
//            // 工单特殊处理
//            WorkOrderEntity workOrderEntity = workOrderModel.getByKey(dataId, corpid);
//            if (Objects.nonNull(workOrderEntity)) {
//                BeanUtil.copyProperties(workOrderEntity, paasFormDataEntityExt);
//            }
//            JSONObject data = paasFormDataEntityExt.getData();
//            data.put(FieldTypeEnum.CLOSETIME.getAlias(),workOrderEntity.getCloseTime());
//            data.put(FieldTypeEnum.ENDTIME.getAlias(),workOrderEntity.getEndTime());
//            paasFormDataEntityExt.setData(data);
//        } else {
//            paasFormDataEntityExt = saasGetHelp.getFormData(dataId,corpid,businessType);
//        }
//
//        return paasFormDataEntityExt;
//    }
//
//
//    @Override
//    public Map<String, FieldAttrEntity> getExplainMap(List<FieldAttrEntity> explainList, Integer businessType, String corpid, Long formId) {
//        Map<String, FieldAttrEntity> explainMap = new HashMap<>(explainList.size());
//        for (FieldAttrEntity fieldAttrEntity : explainList) {
//            if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode()) && Objects.equals(fieldAttrEntity.getSaasAttr(), WorkOrderEnum.STAGE.getSaasAttr())) {
//                if (!Objects.equals(formId, 0L)) {
//                    //工单阶段
//                    WorkOrderTemplateEntity workOrderTemplateEntity = workOrderTemplateService.getByFormId(formId, corpid);
//                    List<ItemPoJo> itemPoJoList = workOrderStageService.getStageNameItemList(workOrderTemplateEntity.getId(), corpid);
//                    fieldAttrEntity.setItems(itemPoJoList);
//                }
//            }
//            if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(),fieldAttrEntity.getFieldType())){
//                SubFormPoJo subForm = fieldAttrEntity.getSubForm();
//                if (subForm != null && subForm.getItems() != null) {
//                    List<? extends FieldAttrEntity> subExplainList = subForm.getItems();
//                    Iterator<? extends FieldAttrEntity> iterator = subExplainList.iterator();
//                    while (iterator.hasNext()){
//                        FieldAttrEntity subFieldAttrEntity = iterator.next();
//                        if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.PRODUCT.getAttr())){
//                            subFieldAttrEntity.setFieldType(FieldTypeEnum.TEXT.getType());
//                            break;
//                        }
//                    }
//                }
//            }
//            explainMap.put(fieldAttrEntity.getAttr(),fieldAttrEntity);
//        }
//        if (Objects.equals(businessType, XbbRefTypeEnum.PRODUCT.getCode())) {
//            explainList.remove(explainMap.get(ProductEnum.IS_NOTIFY.getAttr()));
//            explainMap.remove(ProductEnum.IS_NOTIFY.getAttr());
//            explainList.remove(explainMap.get(ProductEnum.STOCK_NOTIFY.getAttr()));
//            explainMap.remove(ProductEnum.STOCK_NOTIFY.getAttr());
//        }
//        if (Objects.equals(businessType, XbbRefTypeEnum.PAYMENT_SHEET.getCode())) {
//            //此处因为是显示，所以要展示所有的回款单类型
//            List<ItemPoJo> itemList = dataDictionaryModel.getDictionaryList4PaymentSheetType(true);
//            //先移除原类型解释，然后设置后塞入新的
//            for (FieldAttrEntity next : explainList) {
//                String attr = next.getAttr();
//                if (Objects.equals(PaymentSheetEnum.PAYMENT_SHEET_TYPE.getAttr(), attr)) {
//                    next.setItems(itemList);
//                    explainMap.put(PaymentSheetEnum.PAYMENT_SHEET_TYPE.getAttr(), next);
//                }
//            }
//        } else if (Objects.equals(businessType, XbbRefTypeEnum.PAY_SHEET.getCode())) {
//            //此处因为是显示，所以要展示所有的付款单类型
//            List<ItemPoJo> itemList = dataDictionaryModel.getDictionaryList4PaySheetType(true);
//            //先移除原类型解释，然后设置后塞入新的
//            for (FieldAttrEntity next : explainList) {
//                String attr = next.getAttr();
//                if (Objects.equals(PaySheetEnum.PAY_SHEET_TYPE.getAttr(), attr)) {
//                    next.setItems(itemList);
//                    explainMap.put(PaySheetEnum.PAY_SHEET_TYPE.getAttr(), next);
//                }
//            }
//        }
//        return explainMap;
//    }
//
//    @NonNull
//    @Override
//    public List<Integer> businessType() {
//        return Collections.singletonList(0);
//    }
//
//    @Override
//    public void formatListCondition(FormDataListDTO formDataListDTO) throws XbbException {
//        // 先调用父类方法
//        super.formatListCondition(formDataListDTO);
//
//        // 子类实现
//
//        //过滤回款计划中有红冲回款计划的数据，针对回款计划特地写的一个方法
//        formDataListDTO.setShouldConditions(this.setPaymentIsRed(formDataListDTO));
//
//        Integer businessType = formDataListDTO.getBusinessType();
//        String corpid = formDataListDTO.getCorpid();
//        Map<String, FieldAttrEntity> explainMap = formDataListDTO.getExplainMap();
//        List<ConditionsEntityExt> conditions = formDataListDTO.getConditions();
//        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
//        switch (xbbRefTypeEnum) {
//            case PRODUCT:
//                Iterator<ConditionsEntityExt> iterator = conditions.iterator();
//                List<ConditionsEntityExt> childConditionList = new ArrayList<>();
//                while (iterator.hasNext()){
//                    ConditionsEntityExt conditionsEntityExt = iterator.next();
//                    Integer fieldType = conditionsEntityExt.getFieldType();
//                    List<Object> value = conditionsEntityExt.getValue();
//                    String attr = conditionsEntityExt.getAttr();
//
//                    boolean isChildField = ChildProductFieldEnum.isChildField(attr);
//                    if (isChildField){
//                        //子产品字段
//                        if (Objects.equals(attr,ProductEnum.SPECIFICATION.getAttr())){
//                            conditionsEntityExt.setAttr(ProductEnum.SPECIFICATION_LINK_TEXT.getAttr());
//                        }
//                        childConditionList.add(conditionsEntityExt);
//                        iterator.remove();
//                    }
//                    if (Objects.equals(fieldType, FieldTypeEnum.COMBO.getType())
//                                        || Objects.equals(fieldType, FieldTypeEnum.RADIO_GROUP.getType())
//                                        || Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())) {
//                        FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
//                        if (CollectionsUtil.isEmpty(value) || fieldAttrEntity == null) {
//                            continue;
//                        }
//                        if (Objects.equals(ProductEnum.UNIT.getAttr(), fieldAttrEntity.getAttr())) {
//                            List<ItemPoJo> items = fieldAttrEntity.getItems();
//                            try {
//                                ProductFieldPojo productFieldPojo = saasFormHelp.getProductFieldPojo(RedisPrefixConstant.PRODUCT_UNIT, ProductEnum.UNIT.getAttr(), formDataListDTO.getCorpid());
//                                List<MultiUnitItemPoJo> multiItems = productFieldPojo.getMultiItems();
//                                BeanUtil.copyPropertiesList(multiItems, items, ItemPoJo.class);
//                            } catch (Exception e) {
//                                log.error("产品多单位解析错误",e);
//                            }
//                            for (ItemPoJo itemPoJo : items) {
//                                if (Objects.equals(itemPoJo.getIsOther(), BasicConstant.ZERO)) {
//                                    if (Objects.equals(itemPoJo.getText(), value.get(0))) {
//                                        // 因为前端回传的是text,筛选的时候需要将他转换为value值取查询
//                                        conditionsEntityExt.setValue(Collections.singletonList(itemPoJo.getValue()));
//                                        break;
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//                // 规格的常用筛选
//                if (!childConditionList.isEmpty()){
//                    BoolQueryBuilder boolQueryBuilder = boolQuery();
//                    EsUtil.getTermQuery(boolQueryBuilder, formDataListDTO, true);
//                    EsUtil.parseCondition(boolQueryBuilder,childConditionList,"",corpid,XbbRefTypeEnum.PRODUCT.getCode());
//                    boolQueryBuilder.mustNot(termsQuery(ProductEnum.getAttrConnectData(ProductEnum.PARENT_ID), Collections.singletonList(0)));
//                    List<String> childProductFieldList = new ArrayList<>();
//                    childProductFieldList.add(StringConstant.DATA_ID);
//                    childProductFieldList.add(ProductEnum.getAttrConnectData(ProductEnum.PARENT_ID));
//                    List<PaasFormDataEntityExt> childProductList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT,boolQueryBuilder,PaasFormDataEntityExt.class,childProductFieldList);
//                    Set<Long> parentIdList = new HashSet<>();
//                    parentIdList.add(-1L);
//                    for (PaasFormDataEntityExt entityExt : childProductList){
//                        parentIdList.add(entityExt.getData().getLong(ProductEnum.PARENT_ID.getAttr()));
//                    }
//                    conditions.add(new ConditionsEntityExt(FieldTypeEnum.DATAID.getAlias(),"",FieldTypeEnum.DATAID.getType(), ConditionEnum.IN.getSymbol(),new ArrayList<>(parentIdList)));
//                }
//                conditions.add(new ConditionsEntityExt(ProductEnum.PARENT_ID.getAttr(),"",ProductEnum.PARENT_ID.getFieldType(),ConditionEnum.EQUAL.getSymbol(),Collections.singletonList(0)));
//                break;
//            case CUSTOMER_COMMUNICATE:
//                // 排除项目日志
//                conditions.add(new ConditionsEntityExt(FieldTypeEnum.LOG_TYPE.getAlias(),"",FieldTypeEnum.LOG_TYPE.getType(),ConditionEnum.EQUAL.getSymbol(),Collections.singletonList(0)));
//                break;
//            default:
//                break;
//        }
//        formDataListDTO.setConditions(conditions);
//    }
//
//    /**
//     * 设置红冲条件
//     * @param formDataListDTO 入参
//     * @return  List<List<ConditionsEntityExt>>
//     */
//    private List<List<ConditionsEntityExt>> setPaymentIsRed(FormDataListDTO formDataListDTO){
//        List<List<ConditionsEntityExt>> shoudConditions = formDataListDTO.getShouldConditions();
//        List<ConditionsEntityExt> conditions = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        if (!Objects.equals(formDataListDTO.getBusinessType(),XbbRefTypeEnum.PAYMENT.getCode())){
//            return shoudConditions;
//        }
//        ConditionsEntityExt conditionEqualOne = new ConditionsEntityExt(PaymentEnum.IS_RED.getAttr(), "", FieldTypeEnum.NUM.getType(), ConditionEnum.NOLIKE.getSymbol(), Arrays.asList(1));
//        conditions.add(conditionEqualOne);
//        shoudConditions.add(conditions);
//        return shoudConditions;
//    }
//
//
//    @Override
//    public void handleDataList(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO,WorkOrderDataEsListVO workOrderDataEsListVO) throws XbbException {
//        if (Objects.equals(formDataListDTO.getBusinessType(), XbbRefTypeEnum.PRODUCT.getCode())) {
//            AbstractOptionHandle abstractOptionHandle = optionFactory.getByBusinessType(formDataListDTO.getBusinessType(), formDataListDTO.getSaasMark());
//            abstractOptionHandle.setOptionsInData(formDataListDTO, paasFormDataEsListVO);
//        }
//
//        if (Objects.equals(formDataListDTO.getBusinessType(), XbbRefTypeEnum.WORK_ORDER.getCode())) {
//            // 工单列表特殊处理
//            handleSpecial(formDataListDTO, workOrderDataEsListVO, paasFormDataEsListVO);
//        }
//    }
//
//    @Override
//    public void handleGetData(OuterLinkBaseDTO outerLinkBaseDTO, Map<String, FieldAttrEntity> explainMap,
//                              PaasFormDataEntityExt formData, ParseSingleRowDataVO parseSingleRowDataVO,
//                              FormDataUpdateGetVO formDataUpdateGetVO) throws XbbException {
//
//        int saasMark = SaasMarkEnum.SAAS.getCode();
//        Long dataId = outerLinkBaseDTO.getDataId();
//        String corpid = outerLinkBaseDTO.getCorpid();
//        Integer businessType =  outerLinkBaseDTO.getBusinessType();
//        if (Objects.equals(businessType, 0)) {
//            saasMark = SaasMarkEnum.PAAS.getCode();
//        }
//        JSONObject data = formData.getData();
//        if (Objects.equals(saasMark, SaasMarkEnum.SAAS.getCode())) {
//            SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
//            BeanUtil.copyProperties(outerLinkBaseDTO, saasAnalysisDataDTO);
//            // saas特殊格式转换
//            saasAnalysisDataDTO.setExplainMap(explainMap);
//            saasAnalysisDataDTO.setBusinessType(businessType);
//            saasAnalysisDataDTO.setFromOuterLink(BasicConstant.ONE);
//            saasUpdateHelp.formatData(saasAnalysisDataDTO, data);
//
//            if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode())) {
//                if (data.containsKey(CustomerCommunicateEnum.AT_USERNAME.getAttr())) {
//                    JSONArray jsonArray = data.getJSONArray(CustomerCommunicateEnum.AT_USERNAME.getAttr());
//                    if (jsonArray.size() <= 0) {
//                        data.remove(CustomerCommunicateEnum.AT_USERNAME.getAttr());
//                    }
//                }
//            } else if (data.containsKey(SupplierCommunicateEnum.AT_USERNAME.getAttr())) {
//                if (data.containsKey(SupplierCommunicateEnum.AT_USERNAME.getAttr())) {
//                    JSONArray jsonArray = data.getJSONArray(SupplierCommunicateEnum.AT_USERNAME.getAttr());
//                    if (jsonArray.size() <= 0) {
//                        data.remove(SupplierCommunicateEnum.AT_USERNAME.getAttr());
//                    }
//                }
//            } else if (Objects.equals(businessType, XbbRefTypeEnum.RECEIPT_ORDER.getCode())) {
//                // TODO
////                List<String> remove = OuterLinkFilterRemoveAttrEnum.getAttrList(businessType);
////                headList.removeIf(head -> remove.contains(head.getAttr()));
//            }
//
//            // 编辑时saas字段显示特殊逻辑
//            FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
//            BeanUtil.copyProperties(outerLinkBaseDTO, formatExplainDTO);
//            formatExplainDTO.setExplainList(formDataUpdateGetVO.getExplainList());
//            formatExplainDTO.setDataEntity(formData);
//            formatExplainDTO.setResult(data);
//            formatExplainDTO.setFromCopy(false);
//            formExplainHelp.formatExplain(formatExplainDTO, data);
//            // saas特殊返回字段，放在一个对象中
//            saasUpdateHelp.formatSaasAttrToReturn(formDataUpdateGetVO, businessType, data);
//        }
//        boolean multiSpec = false;
//        boolean enableMultiUnit = false;
//        if ( Objects.equals(businessType, XbbRefTypeEnum.PRODUCT.getCode()) ) {
//            // 产品编辑的时候一定要传父产品的id
//            PaasFormDataEntityExt productEntity = saasGetHelp.getFormData(dataId, corpid, XbbRefTypeEnum.PRODUCT.getCode());
//            if ( productEntity == null ) {
//                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
//            }
//            JSONObject productData = productEntity.getData();
//            Long parentId = productData.getLong(ProductEnum.PARENT_ID.getAttr());
//            if ( !Objects.equals(parentId, 0L) ) {
//                dataId = parentId;
//            }
//            // 是否是多单位
//            enableMultiUnit = Objects.equals(productData.getInteger(ProductEnum.ENABLE_MULTI_UNIT.getAttr()), BasicConstant.ONE);
//            // 是否是多规格
//            Integer advancedMode = getIntegerOrDefaultFromFormData(productData, ProductEnum.SPECIFICATION.getAttr(), 0);
//            multiSpec = Objects.equals(advancedMode, 1);
//        }
//        JSONObject result = parseSingleRowDataVO.getResult();
//        if ( Objects.equals(businessType, XbbRefTypeEnum.INVOICE.getCode()) ) {
//            SaasSpecialParamPojo saasSpecialParamPojo = new SaasSpecialParamPojo();
//            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//            param.put("invoiceId", dataId);
//            param.put("isCancel", result.getInteger(InvoiceEnum.IS_CANCEL.getAttr()));
//            param.put("del", DelEnum.NORMAL.getDel());
//            List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(param);
//
//            Integer isRed = getIntegerOrDefaultFromFormData(data,InvoiceEnum.IS_RED.getAttr(),BasicConstant.ZERO);
//            JSONArray amountDetail = saasListHelp.getMoneyDetail(invoiceRelationshipEntityList, result, dataId, corpid, 0, BasicConstant.ONE,isRed);
//            result.put(PaasConstant.AMOUNTDETAIL, amountDetail);
//            if(Objects.equals(isRed,BasicConstant.ONE)) {
//                saasSpecialParamPojo.setFormRedEdit(BasicConstant.ONE);
//            }
//
//            formDataUpdateGetVO.setSaasSpecialParamPojo(saasSpecialParamPojo);
//        }
//        if (Objects.equals(businessType, XbbRefTypeEnum.PRODUCT.getCode())) {
//            // 因为前端用编辑页面来处理外链分享详情页，产品分类在编辑产品时会请求接口查询分类
//
//            ChildProductDTO childProductDTO = new ChildProductDTO();
//            childProductDTO.setCorpid(corpid);
//            childProductDTO.setParentProductId(Collections.singletonList(dataId));
//            PaasFormDataEsListVO esDataList = productService.getChildProductList(childProductDTO);
//            List<PaasFormDataEntityExt> childProductList = esDataList.getPaasFormDataESList();
//            List<PaasFormDataEntityExt> multiProductList = new ArrayList<>();
//            Map<Long, Object> multiProductsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//            // 所以这边将分类改成数字类型，并且这边将数据渲染好，就不会让分类字段额外查询接口了
//            List<FieldAttrEntity> explains = formDataUpdateGetVO.getExplainList();
//            for (FieldAttrEntity fieldAttrEntity : explains) {
//                String attr = fieldAttrEntity.getAttr();
//                if (Objects.equals(attr, ProductEnum.CATEGORY_ID.getAttr())) {
//                    fieldAttrEntity.setFieldType(FieldTypeEnum.TEXT.getType());
//                    Long categoryId = null;
//                    if (Objects.nonNull(result)) {
//                        String name = "";
//                        try {
//                            categoryId = result.getLong(ProductEnum.CATEGORY_ID.getAttr());
//                            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//                            map.put("corpid",corpid);
//                            map.put("del",0);
//                            map.put("id",categoryId);
//                            List<ProductCategoryEntity> categoryEntityList = productCategoryModel.findEntitys(map);
//                            if (CollectionsUtil.isNotEmpty(categoryEntityList)) {
//                                name = categoryEntityList.get(0).getName();
//                            }
//                        } catch (Exception e) {
//                            log.error("outerLinkServiceImpl.get解析产品外链分享分类出错", e);
//                        }
//                        if(categoryId!=null){
//                            result.put(ProductEnum.CATEGORY_ID.getAttr(), name);
//                        }
//
//                    }
//                } else if(Objects.equals(attr, ProductEnum.COST.getAttr())){
//                    String priceStr;
//                    if (multiSpec) {
//                        // 将子产品按照价格排序
//                        priceStr = productService.getPriceOrCostRegion(childProductList, attr, fieldAttrEntity.getAccuracy());
//                    } else {
//                        Double price = getDoubleOrDefaultFromFormData(childProductList.get(0).getData(), attr, 0D);
//                        priceStr = attrDecimalPrecisionHelper.formatMoneyPrecision(price, fieldAttrEntity);
//                    }
//                    result.put(attr, priceStr);
//                } else if (Objects.equals(attr, ProductEnum.PRICE.getAttr())){
//                    int priceAccuracy = fieldAttrEntity.getAccuracy() == null ? AttrDecimalPrecisionHelper.DEFAULT_PRECISION : fieldAttrEntity.getAccuracy();
//                    if (enableMultiUnit) {
//                        productService.handleMultiPrice(multiProductsMap, multiProductList,corpid, dataId, priceAccuracy);
//                    }
//                    String priceStr;
//                    if (enableMultiUnit) {
//                        //多单位产品都需要做价格排序
//                        priceStr = productService.getPriceOrCostRegion(multiProductList, attr, fieldAttrEntity.getAccuracy());
//                    } else if (multiSpec) {
//                        // 将子产品按照价格排序
//                        priceStr = productService.getPriceOrCostRegion(childProductList, attr, fieldAttrEntity.getAccuracy());
//                    } else {
//                        Double price = getDoubleOrDefaultFromFormData(childProductList.get(0).getData(), attr, 0D);
//                        priceStr = attrDecimalPrecisionHelper.formatMoneyPrecision(price, fieldAttrEntity);
//                    }
//                    result.put(attr, priceStr);
//                }
//            }
//        }
//        if(Objects.equals(businessType, XbbRefTypeEnum.PURCHASE_INVOICE.getCode())){
//            PurchaseInvoiceAmountDetailGetDTO purchaseInvoiceAmountDetailGetDTO = InvoiceCommonHelp.getAmountDetailByInvoiceData(formData);
//            BeanUtil.copyProperties(outerLinkBaseDTO,purchaseInvoiceAmountDetailGetDTO);
//            purchaseInvoiceAmountDetailGetDTO.setIsEdit(BasicConstant.ONE);
//            JSONArray originObject = data.getJSONArray(PurchaseInvoiceEnum.ORIGIN_ID.getAttr());
//            Long originId = 0L;
//            if(Objects.nonNull(originObject)){
//                originId = originObject.getJSONObject(BasicConstant.ZERO).getLong(BasicConstant.ID);
//            }
//
//            if(originId > 0L){
//                purchaseInvoiceAmountDetailGetDTO.setFormRed(BasicConstant.ONE);
//            }
//            PurchaseInvoiceAmountDetailVO purchaseInvoiceAmountDetailVO = purchaseInvoiceService.getPurchaseInvoiceAmountDetail(purchaseInvoiceAmountDetailGetDTO);
//            if(Objects.equals(purchaseInvoiceAmountDetailGetDTO.getIsEdit(),BasicConstant.ONE) && Objects.equals(purchaseInvoiceAmountDetailGetDTO.getIsRed(),BasicConstant.ONE)){
//                if (Objects.nonNull(result)) {
//                    double invoiceAmount = result.getDouble(PurchaseInvoiceEnum.TOTAL_MONEY.getAttr());
//                    result.put(PurchaseInvoiceEnum.TOTAL_MONEY.getAttr(), -invoiceAmount);
//                }
//            }
//            JSONArray amountDetail = JSONArray.parseArray(JSONArray.toJSONString(purchaseInvoiceAmountDetailVO.getAmountDetail()));
//            JSONArray paySheetArray = data.getJSONArray(PurchaseInvoiceEnum.PAY_SHEET_ID.getAttr());
//            List<Long> paySheetiList = new ArrayList<>();
//            if(CollectionsUtil.isNotEmpty(paySheetArray)){
//                for(int i=0;i<paySheetArray.size();i++){
//                    JSONObject paySheetObject = paySheetArray.getJSONObject(i);
//                    paySheetiList.add(paySheetObject.getLong(BasicConstant.ID));
//                }
//                Map<Long,String> paySheetTypeMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//                Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//                map.put("corpid",corpid);
//                map.put("idIn",paySheetiList);
//                map.put("del", BasicConstant.ZERO);
//                List<PaasFormDataEntityExt> paySheetEntityList = payPlanSheetModel.findEntitys(map);
//                paySheetEntityList.forEach(item -> {
//                    JSONObject jsonObject = item.getData();
//                    String paySheetType = jsonObject.getString(PaySheetEnum.PAY_SHEET_TYPE.getAttr());
//                    paySheetTypeMap.put(item.getId(),paySheetType);
//                });
//                for(int i=0;i<paySheetArray.size();i++){
//                    JSONObject paySheetObject = paySheetArray.getJSONObject(i);
//                    String paySheetType = paySheetTypeMap.getOrDefault(paySheetObject.getLong(BasicConstant.ID),"");
//                    paySheetObject.put(InvoiceConstant.PAY_SHEET_TYPE,paySheetType);
//                }
//            }
//            result.put(PaasConstant.AMOUNTDETAIL, amountDetail);
//        }
//
//        //现在前端只需要计算重复金额：重复金额为回款计划的已收款部分
//        if ( Objects.equals(businessType, XbbRefTypeEnum.CONTRACT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.PURCHASE.getCode())) {
//            // TODO 这个什么东西
//            //Double writeOffAmount = fundMoneyHelp.getCoverCalAmount(outerLinkDTO,explainMap,businessType);
//            formDataUpdateGetVO.setWriteOffAmount(0D);
//        }
//        if(Objects.equals(businessType, XbbRefTypeEnum.PURCHASE.getCode())){
//            boolean isPurchaseProductMustBelongSupplier = Objects.equals(FormConfigEnum.PURCHASE_PRODUCT_MUST_BELONG_SUPPLIER.getConfigValue(), StringConstant.POSITIVE_NUMBER);
//            //可能限制供应商
//            FormConfigEntity formConfigEntity = formConfigModel.getEntityByFormId(corpid,outerLinkBaseDTO.getFormId(), FormConfigEnum.PURCHASE_PRODUCT_MUST_BELONG_SUPPLIER.getConfig());
//            if (Objects.nonNull(formConfigEntity)) {
//                if (Objects.equals(formConfigEntity.getConfigValue(), StringConstant.POSITIVE_NUMBER)) {
//                    isPurchaseProductMustBelongSupplier = true;
//                } else {
//                    isPurchaseProductMustBelongSupplier = false;
//                }
//            }
//            JSONObject saasObj = new JSONObject();
//            saasObj.put("purchaseProductBelongSupplier", isPurchaseProductMustBelongSupplier);
//            formDataUpdateGetVO.setSaasObj(saasObj);
//        }
//
//        if (Objects.equals(businessType, XbbRefTypeEnum.COMMUNICATE_PLAN.getCode())) {
//            // 访客计划不支持编辑，所以外链分享详情页没有编辑按钮
//            formDataUpdateGetVO.setHasEditPermission(BasicConstant.ZERO);
//        }
//
//
//    }
//
//    @Override
//    public FormDataAddVO addForWorkOrder(OuterLinkAddDTO outerLinkAddDTO,FormDataAddVO formDataAddVO) throws XbbException {
//        //工单逻辑与其他业务逻辑分离，在workOrderFormDataService.add中已加异常后的es删除相关数据操作 TODO 待处理
//        try {
//            if ( outerLinkAddDTO.getBusinessType() ==  XbbRefTypeEnum.WORK_ORDER.getCode() ) {
//                //工单
//                formDataAddVO = workOrderFormDataService.addForOutLink(outerLinkAddDTO);
//                return formDataAddVO;
//            }
//        } catch (XbbException e) {
//            throw e;
//        } catch (Exception e) {
//            log.error("workOrderFormData 数据库保存表单数据出错", e);
//            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
//        }
//        return formDataAddVO;
//    }
//
//    @Override
//    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
//        saasSaveHelp.beforeSave(validateDataDTO);
//    }
//
//    @Override
//    public AfterSaveVO insert(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException {
//        return proModelService.insert(formDataAddDTO, paasFormDataEntity, paasFormEntityExt);
//    }
//
//    @Override
//    public FormDataAddGetVO addGetForWorkOrder(OuterLinkAddGetDTO outerLinkAddGetDTO, FormDataAddGetVO vo) throws XbbException {
//        //工单
//        vo = workOrderFormDataService.addGetForOutLink(outerLinkAddGetDTO);
//        return vo;
//    }
//
//
//    private void handleSpecial(FormDataListDTO formDataListDTO, WorkOrderDataEsListVO workOrderDataEsListVO, PaasFormDataEsListVO paasFormDataEsListVO) {
//        String corpid = formDataListDTO.getCorpid();
//        Set<Long> templateIds = new HashSet<>();
//        Set<Long> workOrderIdList = new HashSet<>();
//        List<WorkOrderEntityExt> workOrderEntityExts = workOrderDataEsListVO.getPaasFormDataESList();
//        for (WorkOrderEntityExt workOrderEntityExt : workOrderEntityExts) {
//            Long workOrderId = workOrderEntityExt.getId();
//            workOrderIdList.add(workOrderId);
//            templateIds.add(workOrderEntityExt.getTemplateId());
//        }
//        Map<Long, WorkOrderTemplateEntity> templateMap = workOrderTemplateModel.getTemplateMap(templateIds, corpid);
//
//        List<PaasFormDataEntityExt> paasFormDataESList = paasFormDataEsListVO.getPaasFormDataESList();
//        Map<Long, PaasFormDataEntityExt> paasFormDataMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
//            paasFormDataMap.put(paasFormDataEntityExt.getId(), paasFormDataEntityExt);
//        }
//
//        //用于存储阶段名称
//        Map<Long,String> workOrderStageNameMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        List<WorkOrderStageEntity> workOrderStageEntityList = workOrderStageService.getStagesByTemplateIdIn(templateIds, corpid, false);
//        for (WorkOrderStageEntity entity : workOrderStageEntityList) {
//            workOrderStageNameMap.put(entity.getId(), entity.getName());
//        }
//
//        for (WorkOrderEntityExt entity : workOrderEntityExts){
//            WorkOrderTemplateEntity workOrderTemplateEntity = templateMap.get(entity.getTemplateId());
//            if (Objects.isNull(workOrderTemplateEntity)) {
//                log.error("工单外链查询获取工单模板出错 dataId = " + entity.getId());
//                continue;
//            }
//            PaasFormDataEntityExt entityExt = paasFormDataMap.get(entity.getId());
//            if (Objects.isNull(entityExt)) {
//                continue;
//            }
//            //不限时自由工单
//            boolean isLimit = Objects.equals(workOrderTemplateEntity.getIsFree(), 1) && Objects.equals(workOrderTemplateEntity.getExpectedTime(), 0);
//
//            JSONObject orderData = entity.getData();
//            JSONObject formData = entityExt.getData();
//            //老数据data中没有存阶段状态
//            if (orderData.containsKey(WorkOrderEnum.NODE_STATUS.getAttr())){
//                formData.put(FieldTypeEnum.WORK_ORDER_NODE_STATUS.getAlias(), WorkOrderNodeStatusEnum.getNameByCode(orderData.getInteger(WorkOrderEnum.NODE_STATUS.getAttr()),""));
//            } else {
//                formData.put(FieldTypeEnum.WORK_ORDER_NODE_STATUS.getAlias(), "");
//            }
//            formData.put(FieldTypeEnum.WORK_ORDER_STAGE_NAME.getAlias(),workOrderStageNameMap.getOrDefault(entity.getNowStageId(), ""));
//            if (isLimit) {
//                //data.put(WorkOrderEnum.TIME_OUT.getAttr(), "");
//                TimeConsumingOutPojo timeConsumingOutPojo =  workOrderBasicService.workOrderSpecialConsueTime(entity);
//                formData.put(WorkOrderEnum.TIME_OUT.getAttr(), timeConsumingOutPojo.getConsumeTimeStr());
//            } else {
//                TimeConsumingOutPojo timeConsumingOutPojo = workOrderBasicService.workOrderConsueTime(entity);
//                formData.put(WorkOrderEnum.TIME_OUT.getAttr(), timeConsumingOutPojo.getConsumeTimeStr());
//            }
//            if (Objects.equals(formDataListDTO.getPlatform(), PlatFormEnum.WEB.getValue())) {
//                formData.put(FieldTypeEnum.CLOSETIME.getAlias(), entity.getCloseTime());
//                formData.put(FieldTypeEnum.ENDTIME.getAlias(), entity.getEndTime());
//            } else {
//                if (Objects.nonNull(entity.getCloseTime()) && entity.getCloseTime() > 0) {
//                    formData.put(FieldTypeEnum.CLOSETIME.getAlias(), DateTimeUtil.getStringEpochSecond(entity.getCloseTime(), DateTimeUtil.SDFYMDHM));
//                }
//                if (Objects.nonNull(entity.getEndTime()) && entity.getEndTime() > 0) {
//                    formData.put(FieldTypeEnum.ENDTIME.getAlias(), DateTimeUtil.getStringEpochSecond(entity.getEndTime(), DateTimeUtil.SDFYMDHM));
//                }
//            }
//            formData.put(FieldTypeEnum.WORK_ORDER_STATUS.getAlias(),WorkOrderNodeStatusEnum.getByCode(entity.getStatus()).getName());
//        }
//    }
//
//
//
//
//}
